package view;

import back.Head;
import back.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static back.Head.*;

public class Login {
    private JPanel panel = new JPanel();
    private JTextField  ip = new JTextField(16);
    private JPasswordField password = new JPasswordField(16);
    private JButton loginButton = new JButton("登录");
    private JButton registerButton = new JButton("注册");

    public Login() {
        panel.add(new JLabel("用户登录",JLabel.CENTER));
        panel.add(new JLabel("账号："));
        panel.add(ip);
        panel.add(new JLabel("密码："));
        panel.add(password);
        panel.add(loginButton);
        panel.add(registerButton);
        JLabel t = new JLabel();

        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                User user = mySQL_User.getUserByIP(ip.getText());

                if(user!=null && user.getPassword().equals(new String(password.getPassword()))) {

                    container.removeAll();
                    container.revalidate();
                    container.repaint();
                    frame.setLayout(new BorderLayout());
                    container.add(manager.GetPanel());
                    frame.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(panel, "登录失败！账户或密码错误！");
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                container.removeAll();
                container.add(register.GetPanel());
                container.revalidate();
                container.repaint();
                frame.setVisible(true);
            }
        });
    }

    public JPanel GetPanel(){
        panel.setLayout(new GridLayout(10,1,0,15));
        return panel;
    }
}
